package com.kaibes.admin.permission;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kaibes.core.base.annotation.Description;
import com.kaibes.module.permission.db.entity.Power;
import com.kaibes.module.permission.db.service.PowerService;
import com.kaibes.module.permission.db.service.RoleService;
import com.kaibes.web.admin.AdminController;
import com.kaibes.web.admin.AdminControllerUtils;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ApiLinkService;

@AdminController
@RequestMapping("/admin/permission")
public class PermissionController {

    @Autowired
    private AdminControllerUtils controllerUtils;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PowerService powerService;
    @Autowired
    private ApiLinkService apiLinkService;

    @ApiLink("角色管理")
    @GetMapping("role")
    public String adminRole(Model model) {
        model.addAttribute("roleList", roleService.list());
        model.addAttribute("permissionList", powerService.list());
        return "permission-role";
    }

    @ApiLink("查看权限")
    @GetMapping("power")
    public String adminRoleSet(Model model, @RequestParam @Description("角色ID") Integer roleId) {
        model.addAttribute("roleId", roleId);
        model.addAttribute("roleName", roleService.getNameById(roleId));
        List<Power> powers = powerService.listByRoleId(roleId);
        model.addAttribute("powers", powers);
        model.addAttribute("apiLinkList", apiLinkService.list(true));
        controllerUtils.setMenu(model, "/admin/permission/role");
        return "permission-power";
    }

}
